package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.mapper.EmployeeMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@AllArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {
    private final EmployeeMapper employeeMapper;
    private final JwtProperties jwtProperties;


    /**
     * 登录
     *
     * @param employeeLoginDTO 条件
     * @return Result
     */
    @Override
    public Result<EmployeeLoginVO> login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        Employee employee = employeeMapper.login(username);
        if ( employee == null ) {
            //账户不存在
            return Result.error("用户不存在");
        }
        if ( !password.equals(employee.getPassword()) ) {
            //密码错误
            return Result.error("密码错误");
        }
        if ( employee.getStatus() == StatusConstant.DISABLE ) {
            //账号被锁定
            return Result.error("账号被锁定");
        }

        //登录成功生成JWT令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    /**
     * 修改密码
     *
     * @param passwordEditDTO 条件
     */
    @Override
    public void updatePassword(PasswordEditDTO passwordEditDTO) {
        passwordEditDTO.setEmpId(BaseContext.getCurrentId());
        employeeMapper.updatePassword(passwordEditDTO);
    }

    /**
     * 启用/修改员工账号
     *
     * @param status 员工
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        employeeMapper.updateStatus(status, id);
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO 分页条件
     * @return PageBean
     */
    @Override
    public PageBean pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page page = employeeMapper.pageQuery(employeePageQueryDTO);
        return new PageBean(page.getTotal(), page.getResult());
    }

    /**
     * 新增员工
     *
     * @param employee 员工信息
     */
    @Override
    public void add(Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employee.setStatus(0);
//        employee.setCreateUser(BaseContext.getCurrentId());
//        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.add(employee);
    }

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return Employee
     */
    @Override
    public Employee selectById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    /**
     * 修改员工信息
     *
     * @param employee 员工信息
     */
    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateEmployee(employee);
    }
}
